var _v = {
	fromEvent: {
		input: require(path.join(__dirname, 'input.js')),
		// checkbox: require(path.join(__dirname, 'checkbox.js')),
		// inputDate: require(path.join(__dirname, 'inputDate.js')),
		// inputSelect: require(path.join(__dirname, 'inputSelect.js')),
		// radio: require(path.join(__dirname, 'radio.js')),
		select: require(path.join(__dirname, 'select.js')),
		textarea: require(path.join(__dirname, 'textarea.js')),
	},
	// popup: Require('/component/popup'),
};
var formBox = function(options, parent) {
	this.options = {
		lable: '',
		annotate: '',
		stutas: 'show',
		size: 'middle',
		layoutName: 'name',
		className: '',
	};
	this.options = $.extend({}, this.options, options);
	this.parent = parent;
	var _lable = $('<div class="form_lable">' + this.options.lable + '</div>');
	this._Box = $('<div class="form_item_layout"></div>');
	this._error = $('<span class="form_error" style="display:none"></span>');
	this._annotate = $('<div class="form_annotate">' + this.options.annotate + '</div>')
	this.html = $('<div class="form_Item_box ' + this.options.className + ' ' + this.options.size + '"></div>');
	this.html.append(_lable);
	this.html.append(this._Box);
	this.html.append(this._error);
	this.html.append('<div style="clear:both"></div>')
	this.html.append(this._annotate);

	if (this.options.stutas != 'show') {
		this.hide();
	};
};
formBox.prototype = {
	constructor: this,
	render: function() {
		return this.html;
	},
	append: function(data) {
		this._Box.append(data);
	},
	showError: function(data) {
		this._error.text(data);
		this._error.css('display', 'block');
		this.html.addClass('form_error_type');
	},
	hideError: function() {
		this._error.css('display', 'none').text('');
		this.html.removeClass('form_error_type');
	},
	show: function() {
		this.options.stutas = 'show';
		this.html.css('display', 'block');
	},
	hide: function() {
		this.options.stutas = 'hide';
		this.html.css('display', 'none');
	}
};
var form = function(options) {
	this.options = options;
	this.html = $('<div class="form_box"></div>');
	this.items = {};
	for (var i = 0; i < this.options.length; i++) {
		var formItemOption = this.options[i];
		if (formItemOption.type == 'checkbox') {
			var layout = new formBox(formItemOption.boxSetting);
			new _v.fromEvent.checkbox(formItemOption.elementSetting, layout, this.items);
			this.html.append(layout.render());
		} else {
			this.items[formItemOption.key] = new _v.fromEvent[formItemOption.type](formItemOption.elementSetting, new formBox(formItemOption.boxSetting));
			this.html.append(this.items[formItemOption.key].layout.render());
		};
	};
	for (var i = 0; i < this.options.length; i++) {
		var formItemOption = this.options[i];
		if (formItemOption.hasOwnProperty('relevance')) {
			if (formItemOption.relevance.event && formItemOption.relevance.relevanceKey) {
				this.items[formItemOption.key].bindRelevance(this.items[formItemOption.relevance.event], formItemOption.relevance.relevanceKey);
			};
		};
	};
};
form.prototype = {
	constructor: this,
	render: function() {
		return this.html;
	},
	set: function(data, doChange) {
		for (var key in data) {
			if (this.items.hasOwnProperty(key) && data.hasOwnProperty(key)) {
				this.items[key].set(data[key], doChange);
			}
		}
	},
	get: function() {
		var data = {};
		var isComplete = true;
		var stutas = false;
		for (var key in this.items) {
			if (this.items.hasOwnProperty(key)) {
				var element = this.items[key];
				if (element.layout && element.layout.options.stutas != 'hide') {
					element.get(function(error, result) {
						if (!error) {
							data[key] = result;
						} else {
							if (!stutas) {
								stutas = error;
							};
							isComplete = false;
						};
					});
				}
			}
		};
		if (isComplete) {
			return data;
		} else {
			alert(stutas);
			return false;
		};
	}
};
module.exports.form = function(options) {
	return new form(options);
};
module.exports.input = function(options) {
	return _v.fromEvent.input(options);
};
module.exports.select = function(options) {
	return _v.fromEvent.select(options);
};
module.exports.textarea = function(options) {
	return _v.fromEvent.textarea(options);
};